package com.rayboo.web.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 
 * LoginController
 * 
 * @author ray
 * @date 2016年9月8日 下午8:02:57
 *
 */
@Controller
public class LoginController {
	
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(HttpServletRequest request,HttpServletResponse response, Model model) {
        model.addAttribute("hasError",request.getParameter("error")!=null);
        model.addAttribute("logout",request.getParameter("logout")!=null);
        return "login";
    }
    
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout(HttpServletRequest request, HttpServletResponse response, Model model) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth != null){
            new SecurityContextLogoutHandler().logout(request, response, auth);
        }
        return "redirect:/login?logout";
    }
}
